import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.math.BigInteger;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class IBanFrame extends JFrame {

    /**
     *
     */
    private static final long serialVersionUID = 4186714844152376779L;
    private JTextField landPrefix;
    private JTextField kontonummerTxt;
    private JTextField bankleitzahlTxt;
    private JTextField ibanTxt;
    private JButton btnBerechne;
    
    public IBanFrame() {
        try {
			setIconImage(ImageIO.read(IBanFrame.class.getResource("img//icon.png")));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("?");
        JMenuItem aboutMenuItem = new JMenuItem("About");
        aboutMenuItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Entwickelt von Florian und Jannis! :)", "About...", JOptionPane.INFORMATION_MESSAGE);
			}
        	
        });
        
        menu.add(aboutMenuItem);
        menuBar.add(menu);
        
        setLayout(new BorderLayout());
        
        this.add(menuBar, BorderLayout.NORTH);
        
        JPanel content = new JPanel();
        content.setLayout(new GridLayout(5, 2));
        
        kontonummerTxt = new JTextField(20);
        kontonummerTxt.setText("970375700");
        bankleitzahlTxt = new JTextField(20);
        bankleitzahlTxt.setText("20080000");
        ibanTxt = new JTextField(20);
        ibanTxt.setEditable(false);
        btnBerechne = new JButton("Berechne!");
        landPrefix = new JTextField(20);
        landPrefix.setText("DE");
        landPrefix.setEditable(false);
       
        content.add(new JLabel("Land:"));
        content.add(landPrefix);
        content.add(new JLabel("Kontonummer:"));
        content.add(kontonummerTxt);
        content.add(new JLabel("Bankleitzahl:"));
        content.add(bankleitzahlTxt);
        content.add(new JLabel("IBAN:"));
        content.add(ibanTxt);
        content.add(new JLabel());
        content.add(btnBerechne);
        
        this.add(content, BorderLayout.CENTER);
       
        btnBerechne.addActionListener(new ButtonHandler());
    }

   
    private class ButtonHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getSource() == btnBerechne){
                String iban;
               
                String kontonummer = kontonummerTxt.getText();
                String blz = bankleitzahlTxt.getText();
               
                while(kontonummer.length() < 10){
                    kontonummer = "0" + kontonummer;
                }
               
                String laendercode = Laendercodes.getLandCode(landPrefix.getText());
                
                iban = blz + kontonummer + laendercode + "00";               
               
                BigInteger ibanno = new BigInteger(iban);
                BigInteger moduluIban = ibanno.mod(new BigInteger("97"));
               
                BigInteger pruefziffer = new BigInteger("98").subtract(moduluIban);
               
                ibanTxt.setText(landPrefix.getText() + pruefziffer + blz + kontonummer);
               
            }
        }
       
    }
    
    public static void main(String[] args) {
    	
        IBanFrame frame = new IBanFrame();
        frame.pack();
        frame.setTitle("IBAN Rechner!");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
    }
    

}